package com.example.b_308.medical.controller;

import android.provider.Settings;

import com.example.b_308.medical.R;
import com.example.b_308.medical.comment.utils.Global;
import com.example.b_308.medical.comment.utils.PhoneValidator;
import com.example.b_308.medical.controller.base.BaseController;
import com.example.b_308.medical.model.Staff;
import com.example.b_308.medical.service.UserService;
import com.example.b_308.medical.view.user.IUserView;

/**
 * Created by B-308 on 2017/11/28.
 */
public class UserController extends BaseController {
    IUserView iUserView;
    UserService userService;
    static Staff staff = new Staff();

    public UserController(IUserView iUserView) {
        this.iUserView = iUserView;
        userService = new UserService(iUserView);
    }

    public void loginDefault() {
        userService.loginDefault();
    }

    public void login(String phonenumber, String password) {
        if (!PhoneValidator.isPhoneNumber(phonenumber)) {
            iUserView.fail(Global.getString(R.string.phone_error));
            return;
        }
        if (password.length() < 6 || password.length() > 13) {
            iUserView.fail(Global.getString(R.string.password_error));
            return;
        }
        staff.put("phonenumber",phonenumber);
        staff.put("password", password);
        userService.login(staff);
    }

    public void register(String doctor_code,String phone_code,String password,String password2) {
        if (phone_code.length() != 6) {
            iUserView.fail(Global.getString(R.string.phone_code_error));
            return;
        }
        if (password.length() < 6 || password.length() > 13) {
            iUserView.fail(Global.getString(R.string.password_error));
            return;
        }
        if (!password.equals( password2)) {
            iUserView.fail(Global.getString(R.string.password2_error));
            return;
        }

        staff.getPhone();
        staff.put("doctor_code", doctor_code);
        staff.put("phone_code",phone_code);
        staff.put("password", password);
        staff.getAttrs().remove("type");
        userService.register(staff);
    }

    public void getCode(String phonenumber ,int type) {
        if (!PhoneValidator.isPhoneNumber(phonenumber)) {
            iUserView.fail(Global.getString(R.string.phone_error));
            return;
        }
        staff.put("phonenumber",phonenumber);
        staff.put("type",type);
        userService.getCode(staff);
    }

    public void forgetpwd(String code,String password,String password2){

        if (code.length() != 6) {
            iUserView.fail(Global.getString(R.string.phone_code_error));
            return;
        }

        if (!password.equals( password2)) {
            iUserView.fail(Global.getString(R.string.password2_error));
            return;
        }

        staff.getPhone();
        staff.put("password", password);
        staff.put("code",code);
        staff.getAttrs().remove("type");
        userService.forgetpwd(staff);
    }
}
